package com.ydcode.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串处理
 * @author YUPENG.LI
 * @version 1.0.0.0
 *
 */
public class StringUtil {

	/**
	 * 文件后缀名
	 * 
	 * @param name
	 * @return
	 */
	public synchronized static String getPrefix(String name) {
		return name.substring(0, name.lastIndexOf("."));
	}

	/**
	 * 文件后缀名
	 * 
	 * @param file
	 * @return
	 */
	public synchronized static String getFilePrefix(File file) {
		return file.getName().substring(0, file.getName().lastIndexOf("."));
	}

	/**
	 * 获得文件名称不含后缀
	 * 
	 * @param file
	 * @return
	 */
	public synchronized static String getFileNameNotPrefix(File file) {
		return getFileNameNotPrefix(file.getName());
	}

	/**
	 * 获得文件名称不含后缀
	 * 
	 * @param filePath
	 * @return
	 */
	public synchronized static String getFileNameNotPrefix(String filePath) {
		return filePath.substring(filePath.lastIndexOf("\\") + 1, filePath
				.lastIndexOf("."));
	}

	/**
	 * 获得文件名称
	 * 
	 * @param filePath
	 * @return
	 */
	public synchronized static String getFileName(String filePath) {
		return filePath.substring(filePath.lastIndexOf("\\") + 1);
	}

	/**
	 * 获得文件名称不含后缀
	 * 
	 * @param filePath
	 * @return
	 */
	public synchronized static String getFileParentName(String filePath) {
		return new File(filePath).getParent();
	}

	/**
	 * 判断对象是否为空或字符
	 * 
	 * @param obj
	 * @return
	 */
	public synchronized static boolean isNull(Object obj) {
		return obj == null || obj.equals("");
	}

	/**
	 * 帕斯卡命名法(Pascal)
	 * 
	 * @param name
	 *            名字
	 * @return
	 */
	public synchronized static String toPascal(String name) {
		if (isNull(name) || name.length() == 1) {
			return name;
		} else {
			return name.substring(0, 1).toUpperCase() + name.substring(1);
		}
	}

	/**
	 * 匈牙利名法(simonyi) 转 帕斯卡命名法(Pascal)
	 * 
	 * @param name
	 *            名字
	 * @return
	 */
	public synchronized static String simonyiToPascal(String name) {
		if (isNull(name)) {
			return name;
		} else {
			String[] names = name.split("_");
			name = "";
			for (int i = 0; i < names.length; i++) {
				name += toPascal(names[i]);
			}
		}
		return name;
	}

	/**
	 * 帕斯卡命名法(Pascal)
	 * 
	 * @param name
	 *            名字
	 * @return
	 */
	public synchronized static String[] toPascal(String[] names) {
		for (int i = 0; i < names.length; i++) {
			names[i] = toPascal(names[i]);
		}
		return names;
	}

	/**
	 * 骆驼命名法(camel)
	 * 
	 * @param name
	 * @return
	 */
	public synchronized static String toCamel(String name) {
		if (isNull(name)) {
			return name;
		} else {
			return name.substring(0, 1).toLowerCase() + name.substring(1);
		}
	}

	/**
	 * 匈牙利名法(simonyi) 转 骆驼命名法(camel)
	 * 
	 * @param name
	 * @return
	 */
	public synchronized static String simonyiToCamel(String name) {
		if (isNull(name)) {
			return name;
		} else {
			String[] names = name.split("_");
			name = "";
			for (int i = 0; i < names.length; i++) {
				name += toCamel(names[i]);
			}
		}
		return name;
	}

	/**
	 * 骆驼命名法(camel)
	 * 
	 * @param names
	 * @return
	 */
	public synchronized static String[] toCamel(String[] names) {
		for (int i = 0; i < names.length; i++) {
			names[i] = toCamel(names[i]);
		}
		return names;
	}

	/**
	 * 匈牙利数据库命名法(db_simonyi)
	 * 
	 * @param name
	 * @return
	 */
	public synchronized static String toDBSimonyi(String name) {
		if (isNull(name)) {
			return name;
		} else {
			name = name.replace("DAO", "").replace("Xue", "")
					.replace("xue", "");
			name = toCamel(name);
			// 存放大小字母的索引
			List<Integer> upIndex = new ArrayList<Integer>();
			for (int i = 0; i < name.length(); i++) {
				if ('A' <= name.charAt(i) && name.charAt(i) <= 'Z') {
					upIndex.add(i);
				}
			}
			for (int i = 0; i < upIndex.size(); i++) {
				name = name.substring(0, upIndex.get(i) + i) + "_"
						+ toCamel(name.substring(upIndex.get(i) + i));
			}
		}
		return name;
	}

	/**
	 * 匈牙利数据库命名法(db_simonyi)
	 * 
	 * @param names
	 * @return
	 */
	public synchronized static String[] toDBSimonyi(String[] names) {
		for (int i = 0; i < names.length; i++) {
			names[i] = toDBSimonyi(names[i]);
		}
		return names;
	}

	/**
	 * 根据模板替换
	 * 
	 * @param templateString
	 * @param map
	 * @return
	 */
	public synchronized static String toSwaps(String templateString,
			Map<String, Object> map) {
		for (String key : map.keySet()) {
			templateString = templateString.replace(key,
					map.get(key) == null ? "" : map.get(key).toString());
		}
		return templateString;
	}

	/**
	 * 根据模板替换
	 * 
	 * @param templateString
	 * @param map
	 * @return
	 */
	public synchronized static String toSwaps(String templateString,
			String[] map) {
		try {
			for (int i = 0; i < map.length; i++) {
				String[] tempString = map[i].split(":");
				templateString = templateString.replace(tempString[0],
						tempString.length > 1 ? tempString[1] : "");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return templateString;
	}

	/**
	 * 根据模板自动增长替换字符
	 * 
	 * @param templateString
	 * @param map
	 *            key:value@
	 * @自增字符占位符
	 * @param index
	 *            开始的字符串
	 * @return
	 */
	public synchronized static String toAutoIncrement(String templateString,
			Map<String, String> map, int index) {
		try {
			Pattern p = null;
			Matcher m = null;
			for (String key : map.keySet()) {
				int i = index;
				p = Pattern.compile(key);
				m = p.matcher(templateString);
				while (m.find()) {
					m = p.matcher(m.replaceFirst(map.get(key).replaceAll("@",
							i + "")));
					// System.out.println(m.replaceFirst(""));
					i++;
				}
				templateString = m.replaceFirst("");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return templateString;
	}

	/**
	 * * 根据模板替换
	 * 
	 * @param templateString
	 * @param map
	 * @param eSwaps
	 * @return
	 */
	public synchronized static String toSwaps(String templateString,
			Map<String, Object> map, ESwaps eSwaps) {
		templateString = toSwaps(templateString, map);
		// 处理不同模板的大小写规则
		Map<String, ENaming> sMap = new HashMap<String, ENaming>();
		switch (eSwaps) {
		case Spring:
			sMap.put("id=\"\\.*?\"", ENaming.Camel);
			sMap.put("bean=\"\\.*?\"", ENaming.Pascal);
			sMap.put("name=\"\\.*?\"", ENaming.Camel);
			sMap.put("class=\"\\.*\"", ENaming.Pascal);
			// templateString = toReplace(templateString, sMap);
			break;
		case Struts:

			break;
		case Class:

			break;
		}

		return templateString;
	}

	/**
	 * 替换
	 * 
	 * @param str
	 *            数据源
	 * @param map
	 *            替换字符 key:替换字符 value:替换后
	 * @return
	 */
	public synchronized static String toReplace(String str,
			Map<String, String> map) {
		for (String key : map.keySet()) {
			str = str.replace(key, map.get(key));
		}
		return str;
	}

	/**
	 * 替换
	 * 
	 * @param str
	 *            数据源
	 * @param map
	 *            替换字符 key:替换字符 value:替换后
	 * @param where
	 *            替换条件 PS{ 语法: 不能包含字符 "![字符串]" 必须包含字符 "&[字符串]" 分隔符 ";"}
	 * 
	 * @return
	 */
	public synchronized static String toReplace(String str,
			Map<String, String> map, String where) {
		String[] temps = where.split(";");
		List<String> notStu = new ArrayList<String>();
		List<String> inStu = new ArrayList<String>();
		for (int i = 0; i < temps.length; i++) {
			if (temps[i].startsWith("![")) {
				notStu.add(temps[i].substring(2, temps[i].length() - 1));
			} else if (temps[i].startsWith("&[")) {
				inStu.add(temps[i].substring(2, temps[i].length() - 1));
			}
		}
		for (String string : notStu) {
			if (str.indexOf(string) > 0) {
				return str;
			}
		}
		for (String string : inStu) {
			if (str.indexOf(string) < 0) {
				return str;
			}
		}
		for (String key : map.keySet()) {
			str = str.replace(key, map.get(key));
		}
		return str;
	}

	/**
	 * 模板替换方式
	 * 
	 * @author LYP
	 * 
	 */
	enum ESwaps {
		Spring, Struts, Class, Jsp
	}
}
